import {ajax, accredit} from "./resize.js";
import $ from "jquery";

export const way = {

    _login: (fn) => {
        accredit((openid) => {
            $.ajax({
                url: ajax.login + "?openId=" + openid,
                type: "POST",
                xhrFields: {withCredentials: true},
                success: (res) => {
                    way._typeOf(fn) === "[object function]" && fn(res);
                }
            })
        });

    },
    /**
     *获取元素样式
     * @param obj
     * @param attr
     * @returns {*}
     */
    getStyle: (obj, attr) => {
        if (obj.currentStyle) {
            return obj.currentStyle[attr];
        } else {
            return document.defaultView.getComputedStyle(obj, null)[attr];
        }
    },
    /**
     * 去除px并转换为数值
     * @param str
     * @returns {number}
     */
    pxRemove: (str) => {
        return Number(str.substring(0, str.length - 2));
    },
    //获取滚动条高度
    getScrollTop: () => {
        var scrollTop = 0;
        if (document.documentElement && document.documentElement.scrollTop) {
            scrollTop = document.documentElement.scrollTop;
        }
        else if (document.body) {
            scrollTop = document.body.scrollTop;
        }
        return scrollTop;
    },
    //获取数据类型
    _typeOf: (obj) => {
        return Object.prototype.toString.call(obj).toLowerCase();
    },
    /**
     * 处理图片尺寸
     * @param imgselector
     * @param defselector
     */
    picSizeCtrl: (imgselector, defselector) => {
        if ($(imgselector).parent().parent().css("display") == "block") {
            var img = new Image();
            img.src = $(imgselector).attr("src");
            img.onload = function () {
                var defaultWidth = $(defselector).width(),
                    defaultHeight = $(defselector).height(),
                    trueWidth = img.width,
                    trueHeight = img.height,
                    wrate = trueWidth / defaultWidth,
                    hrate = trueHeight / defaultHeight;
                if (trueWidth <= defaultWidth && trueHeight >= defaultHeight) {
                    $(imgselector).css({
                        "width": "auto",
                        "height": "6.225rem"
                    });
                } else if (trueWidth >= defaultWidth && trueHeight <= defaultHeight) {
                    $(imgselector).css({
                        "width": 11.45 + "rem",
                        "height": "auto"
                    })
                }

                if (trueWidth >= defaultWidth && trueHeight >= defaultHeight) {

                    if (wrate >= hrate) {
                        $(imgselector).css({
                            "width": 11.45 + "rem",
                            "height": "auto"
                        })
                    } else {
                        $(imgselector).css({
                            "width": "auto",
                            "height": "6.225rem"
                        })
                    }
                }

                if (trueWidth <= defaultWidth && trueHeight <= defaultHeight) {
                    if (wrate >= hrate) {
                        $(imgselector).css({
                            "width": 11.45 + "rem",
                            "height": "auto"
                        })
                    } else {
                        $(imgselector).css({
                            "width": "auto",
                            "height": "6.225rem"
                        })
                    }
                }
            }


        }
    }
};
